// pages/search/search.js
// 搜索结果页面
const app = getApp(), core = require('../../utils/core'), util = require('../../utils/util');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    resumes: [],
    resumesMore: {page:1},
    jobs: [],
    jobsMore: {page:1},
    status:0,
    q: '',
    jobSearched: false,
    resumeSearched: false,
  },
  nav:function(e){
    this.setData({
      status:e.currentTarget.dataset.index
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.talent) {
      this.setData({status:1});
    }
    let keywords = options.keywords;
    this.setData({q: keywords, city: app.getCurrentCity()});
    app.eventBus.on(app.Events.CurrentCityChange, this.eventOnCurrentCityChange, this);
    let userinfo = app.getUserinfo();
    if(!userinfo || userinfo.group_id==1) {
      this.setData({status:1});
    }
    this.search();
  },
  onUnload: function () {
    app.eventBus.off(app.Events.CurrentCityChange, this.eventOnCurrentCityChange);
  },
  eventOnCurrentCityChange: function (currentCity) {
    this.setData({ city: currentCity });
  },
  bindKeyInput(event) {
    this.data.q = event.detail.value.trim(); //不用setData，避免影响键盘输入
  },
  search() {
    if(this.data.q==='') {
      wx.showToast({title: '请输入搜索关键词'});
      return;
    }
    this.setData({resumes:[],resumesMore:{page:1}, jobs:[], jobsMore:{page:1}, jobSearched:false, resumeSearched:false});
    //this.fetchResume();
    this.fetchJob();
  },
  // fetchResume() {
  //   util.fetch(this, 'resume/list', {q:this.data.q}, 'resumesMore', 'resumes', 'resumes', function(data){
  //     this.setData({resumeSearched:true});
  //     return data;
  //   });
  // },
  fetchJob() {
    util.fetch(this, 'xilujob.job/lists', {q:this.data.q}, 'jobsMore', 'jobs', 'data', function(data){
      this.setData({jobSearched:true});
      return data;
    });
  }


});
